const regeneratorRuntime = require('../../vendor/regenerator-runtime')
const co = require('../../vendor/co')
const util = require("../../utils/util")
const app = getApp()
const position = {}
const pool = new Map()
let catScrollLeft = 0
Page({
    data: {

        loading: false,
        cid: -1,
        category: [
            {
                id: 0,
                name: "首页"
            }
        ]
    },
    onReady() {
        co(function* () {
            try {
                wx.showLoading({
                    title: '加载中...',
                })
                //分类
                const category = yield app.http.get("index/category", { showMsg: true })
                //banner
                const banner = yield app.http.get("index/shop-banner", { showMsg: true })
                this.setData({
                    category: this.data.category.concat(category.data.categories),
                    banner: banner.data['shop-xcx-banner']
                })
                //分销
                app.checkShare(this.data)
                wx.hideLoading()
                this.clickCategory({
                    currentTarget: {
                        dataset: {
                            id: 0
                        }
                    }
                })
                wx.setNavigationBarTitle({
                    title: wx.getStorageSync('store'),
                })
            } catch (err) {

            }
        }.bind(this))
    },

    onShow() {
        wx.getStorage({
            key: 'store',
            success: function (res) {
                wx.setNavigationBarTitle({
                    title: res.data,
                })
            },
        })
    },
    loadData() {
        let tab = this.getTab()
        if (tab.page > tab.total || tab.loading) {
            return
        }
        tab.loading = true
        this.setData({ loading: true })
        app.http.post("goods/list?page=" + tab.page, {
            data: { category_id: this.data.cid }
        }).then(res => {
            tab.loading = false
            tab.page = tab.page + 1
            tab.total = res.data.total
            tab.list.push(...res.data.goods_list.map(o => {
                if (o.goodsTeams.length) {
                    o.save = o.goodsTeams[0].num + "人团省" + (parseFloat(o.mall_price) - parseFloat(o.goodsTeams[0].price)).toFixed(2)
                    o.mall_price = o.goodsTeams[0].price
                }
                return o
            }))
            this.setData(tab)
            wx.stopPullDownRefresh()
            wx.hideLoading()
        }).catch(err => {
            app.wxToast.error(err.message)
        })
    },

    getTab() {
        if (!pool.has(this.data.cid)) {
            pool.set(this.data.cid, {
                position: 0,
                page: 1,
                total: 2,
                list: [],
                loading: false
            })
        }
        return pool.get(this.data.cid)
    },
    //点击一级分类
    clickCategory(evt) {
        if (this.data.cid != evt.currentTarget.dataset.id) {
            this.setData({
                cid: evt.currentTarget.dataset.id,
            })
            const tab = this.getTab()
            this.setData(tab)
            if (tab.list.length == 0) {
                this.loadData()
            }
            if (wx.pageScrollTo) {
                wx.pageScrollTo({
                    scrollTop: tab.position,
                })
            }
            if (wx.createSelectorQuery) {
                this.setData({
                    hasQuery: true
                })
                const query = wx.createSelectorQuery()
                query.select("#cat" + this.data.cid).boundingClientRect()
                query.select("#cat").boundingClientRect()
                query.exec(res => {
                    let scrollX = res[0].scrollLeft
                    this.setData({
                        tag: {
                            left: res[0].left + catScrollLeft,
                            width: res[0].width,
                        },
                        scrollX
                    })
                })
            }
        }
    },

    onPullDownRefresh: function () {
        const tab = this.getTab()
        tab.page = 1
        tab.total = 2
        tab.list = []
        tab.loading = false
        this.loadData()
    },

    onReachBottom: function () {
        this.loadData()
    },
    onShareAppMessage: function () {
        return app.getShareObj('邀请你来逛逛' + wx.getStorageSync('store'))
    },
    onPageScroll(e) {
        this.getTab().position = e.scrollTop
    },
    onCatScroll(e) {
        catScrollLeft = e.detail.scrollLeft
    }
})
